/**
 * Copyright (C) 2010 Sergey Grigoriev <s.a.grigoriev@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.gmf.dsl

import com.google.inject.Inject
import org.apache.maven.project.MavenProject

import net.gmf.core.FlexProjectUtils
import net.gmf.core.Resolver
import net.gmf.core.ProjectContext

/**
 * DSL for Adobe Flex build
 */
class FlexProject extends BuilderSupport {

  private MavenProject project

  RootNode rootNode = new RootNode()

  MavenProject getProject() { ctx.project }

  final Map props=[:]

  /**
   * Current position in parent-child projects chain
   */
  ProjectContext ctx

  void setCtx(ProjectContext ctx) {
    this.ctx = ctx
    flexProjectUtils = new FlexProjectUtils(ctx.project)
  }


  private FlexProjectUtils flexProjectUtils

  final File getCompilerOutput() { flexProjectUtils.compilerOutput }

  protected void setCurrent(Object current) {
    super.setCurrent(current)
    if (!current) return
    if (current.metaClass.hasProperty(current, 'ctx')) {
      current.ctx = ctx
    }
  }


  protected void setParent(Object parent, Object child) {
  }

  protected Object createNode(Object name) {
    if ('build' == name) {
      return rootNode
    } else {
      current?.createNode(name)
    }
  }

  def protected createNode(Object name, Object value) {
    current?.createNode(name, value)
  }

  protected Object createNode(Object name, Map attributes) {
    current?.createNode(name, attributes)
  }

  protected Object createNode(Object name, Map attributes, Object value) {
    current?.createNode(name, attributes, value)
  }

  def resolve(String dependency) {
    ctx.resolver.resolve(dependency)
  }

  String getBaseDir() {
    project.basedir
  }
}